﻿using System;
using System.Data;
using System.Data.EntityClient;
using System.Linq;
using System.Linq.Expressions;
using ASPItalia.ModelVirtualCasting.Common;
using ASPItalia.ModelVirtualCasting.ObjectModel;

namespace ASPItalia.ModelVirtualCasting.EntityFramework
{
	public class CandidateRepository : BaseRepository<Candidate>, ICandidateRepository
	{
		public CandidateRepository(IContextContainer container) : base(container)
		{
		}

		public void InsertOnSubmit(Candidate entity)
		{
			if (entity.ResidenceCountry != null)
			{
				entity.ResidenceCountryID = entity.ResidenceCountry.CountryID;
				entity.ResidenceCountry = null;
			}
			if (entity.NationalityCountry != null)
			{
				entity.NationalityCountryID = entity.NationalityCountry.CountryID;
				entity.NationalityCountry = null;
			}
			if (entity.JobLevel != null)
			{
				entity.JobLevelID = entity.JobLevel.JobLevelID;
				entity.JobLevel = null;
			}
			else
			{
				entity.JobLevelID = 1;
			}
			if (entity.PrimaryLanguage != null)
			{
				entity.PrimaryLanguageID = entity.PrimaryLanguage.LanguageID;
				entity.PrimaryLanguage = null;
			}
			else
			{
				entity.PrimaryLanguageID = 1;
			}
			if (entity.SecondaryLanguage != null)
			{
				entity.SecondaryLanguageID = entity.SecondaryLanguage.LanguageID;
				entity.SecondaryLanguage = null;
			}
			entity.InsertionDate = DateTime.Now;
			Context.People.AddObject(entity);

			entity.Availability = "N";
		}

		public void Update(Candidate entity)
		{
			Context.People.Attach(entity);
			Context.ObjectStateManager.GetObjectStateEntry(entity).ChangeState(EntityState.Modified);
		}

		public void DeleteOnSubmit(Candidate entity)
		{
			Context.People.DeleteObject(entity);
		}

		public void SubmitChanges()
		{
			SaveChanges();
		}

		public IQueryable<Candidate> GetAll()
		{
			return CurrentObjectQuery.OfTypeOnly<Candidate>();
		}

		public IQueryable<Candidate> Where(Expression<Func<Candidate, bool>> predicate)
		{
			return CurrentObjectQuery.OfTypeOnly<Candidate>().Where(predicate);
		}

		public long Count(Expression<Func<Candidate, bool>> predicate)
		{
			return CurrentObjectQuery.OfTypeOnly<Candidate>().Count(predicate);
		}

		public ICandidateRepository Include(params string[] paths)
		{
			CurrentObjectQuery = CreateObjectQuery().Include(paths);
			return this;
		}

		public void PromoteToModel(string fiscalCode, decimal hourlyRate, string vatCode, DateTime startDate, DateTime endDate, int candidateId)
		{
			EntityCommand command = new EntityCommand("ModelVirtualCastingEntities.PromoteToModel");
			command.CommandType = CommandType.StoredProcedure;
			command.Parameters.AddWithValue("fiscalcode", fiscalCode);
			command.Parameters.AddWithValue("hourlyrate", hourlyRate);
			command.Parameters.AddWithValue("vatCode", vatCode);
			command.Parameters.AddWithValue("startDate", startDate);
			command.Parameters.AddWithValue("endDate", endDate);
			command.Parameters.AddWithValue("candidateid", candidateId);
			Commands.Add(command);
			CreateNewContextAfterSaveChanges = true;
		}

		public Candidate GetByID(int id)
		{
			return CurrentObjectQuery.OfTypeOnly<Candidate>().First(p => p.ID == id);
		}

		public ICandidateRepository EnableLazyLoading()
		{
			SetLazyLoading(true);
			return this;
		}

		public ICandidateRepository DisableLazyLoading()
		{
			SetLazyLoading(false);
			return this;
		}
	}
}